//
// Decompiled by Jadx - 881ms
//
package person.wangchen11.nativeview;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import net.gddhy.openmrp.VKeyBoardView;
import android.inputmethodservice.KeyboardView;
import android.view.View.OnClickListener;
import android.graphics.Bitmap;
import net.yzjlb.sai.mrp.MrpUnpack;

public class MainActivity extends net.gddhy.openmrp.BaseActivity {

	static final int KY_DOWN = 0;
    static final int KY_UP = 1;
    static final int MENU_RET = 5;
    static final int MENU_SLT = 4;
    static final int MR_DIALOG = 6;
    static final int MS_DOWN = 2;
    static final int MS_MOVE = 12;
    static final int MS_UP = 3;

    NativeView mNativeView;
    private RelativeLayout mRelativeLayout = null;
    private View mFakeContentView = null;
	public Bitmap cache = null, real = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
		//不存在dsm时创建dsm
		File dsm = net.gddhy.openmrp.MainActivity.getDSM_GM_File();
		if (!dsm.exists()) {
			try {
				InputStream inputStream = getAssets().open("dsm.mrp");
				net.gddhy.openmrp.MainActivity.streamToDSM(inputStream);
			} catch (IOException e) {

			}
		}

        this.mNativeView = new NativeView(this);
        this.mRelativeLayout = new RelativeLayout(this);
        this.mRelativeLayout.addView(this.mNativeView);
        super.setContentView(this.mRelativeLayout);
        NativeInterface.initActivity(this);
		
		//读取推荐宽高
		MrpUnpack mrp = new MrpUnpack(net.gddhy.openmrp.MainActivity.getDSM_GM_File());
		mrp.updateConfig();
		if(mrp.config.ScreenWidth<240){
			mrp.config.ScreenWidth = 240;
			mrp.config.ScreenHeight = 320;
		}
		
		//设置屏幕缓存
		cache = NativeInterface.createBitmapRgb565(mrp.config.ScreenWidth, mrp.config.ScreenHeight);
		real = NativeInterface.createBitmapRgb565(mrp.config.ScreenWidth, mrp.config.ScreenHeight);
		NativeInterface.setBitmapCache(cache, real);
		
		//屏幕宽高
		NativeInterface.setIntCfg(0,mrp.config.ScreenWidth);
		NativeInterface.setIntCfg(1,mrp.config.ScreenHeight);
		
		//是否启用pthread
		NativeInterface.setIntCfg(2, 1);
		
		//启动之前配置好数据
		NativeInterface.startDsm("%dsm_gm.mrp");
		
		
		//针对多亲1s调整 全屏显示
		if (Build.MODEL.equals("Qin 1s+")) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}

		//虚拟键盘显示
		final VKeyBoardView vKey = new VKeyBoardView(this, null);
		RelativeLayout.LayoutParams lpVkey = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
		lpVkey.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//设置置底
		mRelativeLayout.addView(vKey, lpVkey);
		final Button button = new Button(this);
		button.setText("显示虚拟键盘");
		button.setVisibility(View.GONE);
		mRelativeLayout.addView(button, lpVkey);
		final Vibrator vibrator = (Vibrator)getSystemService(VIBRATOR_SERVICE);
		vKey.setOnKeyboardActionListener(new KeyboardView.OnKeyboardActionListener() {
				@Override
				public void onPress(int i) {
					//按键按下
					vibrator.vibrate(10);
					if (i >= 0) {
						NativeInterface.keyEvent(KY_DOWN, i);
					}
				}

				@Override
				public void onRelease(int i) {
					//按键抬起
					if (i >= 0) {
						NativeInterface.keyEvent(KY_UP, i);
					}
				}

				@Override
				public void onKey(int i, int[] ints) {
					//按键事件
					switch (i) {
						case -1:
							vKey.setVisibility(View.GONE);
							button.setVisibility(View.VISIBLE);
							break;
						case -2:
							new AlertDialog.Builder(MainActivity.this)
								.setTitle("温馨提示")
								.setMessage("你确定要退出吗？\nMrp程序将会终止，注意保存游戏数据")
								.setPositiveButton("确定", new DialogInterface.OnClickListener() {
									@Override
									public void onClick(DialogInterface dialogInterface, int i) {
										finish();
									}
								})
								.setNegativeButton("取消", null)
								.show();
							break;
					}
				}

				@Override
				public void onText(CharSequence charSequence) {

				}

				//按下后滑动
				@Override
				public void swipeLeft() {

				}

				@Override
				public void swipeRight() {

				}

				@Override
				public void swipeDown() {

				}

				@Override
				public void swipeUp() {

				}
			});
		button.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					vKey.setVisibility(View.VISIBLE);
					button.setVisibility(View.GONE);
					
				}
			});
    }

	@Override
	public void finish() {
		super.finish();
		File file = net.gddhy.openmrp.MainActivity.getDSM_GM_File();
		if (file.exists()) {
        	file.delete();
        }
		android.os.Process.killProcess(android.os.Process.myPid());
	}

    @Override
    protected void onPause() {
        NativeInterface.onPause();
        super.onPause();
    }

    @Override
    protected void onResume() {
        NativeInterface.onResume();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        NativeInterface.destroy();
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        if (!NativeInterface.backPressed()) {
            super.onBackPressed();
        }
    }

    @Override
    public void setContentView(View view) {
        if (this.mFakeContentView != null) {
            this.mRelativeLayout.removeView(this.mFakeContentView);
            this.mFakeContentView = null;
        }
        this.mFakeContentView = view;
        this.mRelativeLayout.addView(view, -1, -1);
    }



	public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
		//多亲1s拨号键跳转音量设置
		if (Build.MODEL.equals("Qin 1s+") && keyCode == KeyEvent.KEYCODE_CALL) {
			Intent intent = new Intent("android.settings.SOUND_SETTINGS");
			startActivity(Intent.createChooser(intent, "音量设置"));
			return true;
		}

		int mrpKey = MR_KEY_CODE.androidKeyToMrpKey(keyCode);
		if (mrpKey != MR_KEY_CODE.MR_KEY_ERR) {
        	NativeInterface.keyEvent(KY_DOWN, mrpKey);
        	return true;
		} else {
			return super.onKeyDown(keyCode, keyEvent);
		}
    }

    public boolean onKeyUp(int keyCode, KeyEvent keyEvent) {
		int mrpKey = MR_KEY_CODE.androidKeyToMrpKey(keyCode);
        if (mrpKey != MR_KEY_CODE.MR_KEY_ERR) {
        	NativeInterface.keyEvent(KY_UP, mrpKey);
        	return true;
		} else {
			return super.onKeyUp(keyCode, keyEvent);
		}
    }
}
